package org.example.fronted.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.example.entity.Cart;
import org.example.fronted.mapper.CartMapper1;
import org.example.fronted.service.CartService1;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartService1Impl implements CartService1 {
    @Resource
    private CartMapper1 cartMapper1;


    @Override
    public int addGood(String good_id, Integer u_id) {
        int row = cartMapper1.getGoodByUIdAndGoodId(good_id,u_id);
        int i=0;

        try {
            if (row>0){
               i= cartMapper1.addGood(good_id,u_id);
            }else {
                Cart cart = new Cart();
                cart.setGoodId(good_id);
                cart.setUId(u_id);
                cart.setNum(1);
                cart.setCreateTime(new Date());
                cart.setUpdateTime(new Date());
                i = cartMapper1.insert(cart);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("业务层异常");
        }
        return i;
    }

    @Override
    public int deleteAllGoods(String good_id, Integer u_id) {
        int i=0;
        try{
            i = cartMapper1.deleteAllGoods(good_id, u_id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("业务层异常");
        }
        return i;
    }

    @Override
    public int subGood(Integer good_id, Integer u_id) {
        int i=0;
        try{
            i = cartMapper1.subGood(good_id, u_id);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("业务层异常");
        }
        return i;
    }

    @Override
    public Map<String, Integer> syncCart(Map<String, Integer> cart, Integer uId) {
        QueryWrapper<Cart> queryWrapper =new QueryWrapper<Cart>().eq("u_id",uId);
        List<Cart> goodss = cartMapper1.selectList(queryWrapper);
        if (!cart.isEmpty()) {
            Date time = new Date();
            cart.forEach((k, v) -> {
                if (goodss.contains(k)) {//商品存在
//                    Integer uId, int goodsId, int num, Date updateTime, Date createTime
                    UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<Cart>()
                            .eq("id",uId)
                            .eq("good_id",Integer.parseInt(k))
                            .eq("num",v)
                            .eq("update_time",time);
                    cartMapper1.update(null,updateWrapper);
                } else {
                    Cart c = new Cart();
                    c.setUId(uId);
                    c.setGoodId(k);
                    c.setNum(v);
                    c.setCreateTime(time);
                    cartMapper1.insert(c);

                }
            });
        }

        List<Cart> newGoodss = cartMapper1.selectList(queryWrapper);
        Map<String, Integer> map = new HashMap<String, Integer>();
        newGoodss.forEach(v -> {
            map.put(Integer.toString(Integer.parseInt(v.getGoodId())), v.getNum());
        });
        return map;
    }
}
